package com.logic.landseaserver.models.authcode.ws;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.models.authcode.bean.QueryUsableAuthCodeReq;
import com.logic.landseaserver.models.authcode.service.AuthCodeService;
import com.logic.landseaserver.models.authcode.service.CouponUsedService;
import com.logic.landseaserver.ws.dto.QueryCouponUesdDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

/**
 * Created by Junny on 2017/10/12.
 */
@Path("/codeuesd")
@Api(value = "授权码使用")
public class CodeUesdRest {
    private static final Logger LOGGER = LoggerFactory.getLogger(CodeUesdRest.class);

    @Autowired
    private AuthCodeService authCodeService;

    @Autowired
    private CouponUsedService couponUsedService;
    @POST
    @Path("/queryUsedCode")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "查询可用的授权码")
    public Response queryUsableAuthCode(@ApiParam(value = "param", required = true) QueryUsableAuthCodeReq queryUsableAuthCodeReq)
    {
        CommonResultMap map = new CommonResultMap();
        try {
            List<QueryCouponUesdDTO> result = authCodeService.queryUsableAuthCode(queryUsableAuthCodeReq);
            map.setData(result);
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaException e){
            LoggerHelper.handleException(LOGGER, "queryUsedCode LandseaException", e, map);
        } catch (Exception e){
            LOGGER.info("queryUsedCode Exception" + e.getMessage());
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @DELETE
    @Path("/deleteUesd/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "delete CouponUesd by admin", position = 4)
    public Response deleteUesd(@ApiParam(value = "param", required = true) @PathParam("id") Integer id) {
        CommonResultMap map = new CommonResultMap();
        try {
            couponUsedService.deleteCouponUesdById(id);
            map.setResult(CommonResult.SUCCESS,"delete success");
        } catch (Exception e) {
            LOGGER.error("delete CouponUesd Exception: ", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
